package com.cn.rest.web;

import com.cn.rest.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by chenyuncong on 2019/3/7.
 */
@RestController
@RequestMapping("rest")
public class RestUserController {

    @Autowired
    private RestTemplate restTemplate;

    private static final String getUrl="http://localhost:8080";

    @GetMapping("getRestUser")
    public String getRestUser(User u){


        //无参数 访问返回json字符串
        ResponseEntity<String> user0 = restTemplate.getForEntity(getUrl+"/user/getUser",String.class);
        System.out.println("user0: "+user0.getBody());
        //无参数 访问返回User对象
        ResponseEntity<User> user1 = restTemplate.getForEntity(getUrl+"/user/getUser",User.class);
        System.out.println("user1: "+user1.getBody());

        Map<String,Object> map=new HashMap<>();
        map.put("id",3L);
        map.put("name","小明3");
        map.put("sex","");
        map.put("age",22);
        //get带参数的访问 返回对象
        ResponseEntity<User> user2 = restTemplate.getForEntity(getUrl+"/user/getUserParam?id={id}&name={name}&sex={sex}&age={age}",User.class,map);
        System.out.println("user2: "+user2.getBody());
        //get带参数的访问 返回字符
        ResponseEntity<String> user3 = restTemplate.getForEntity(getUrl+"/user/getUserParam?id={id}&name={name}&sex={sex}&age={age}",String.class,map);
        System.out.println("user3: "+user3.getBody());

        //get 接口是对象做参数 返回对象
        ResponseEntity<User> user4 = restTemplate.getForEntity(getUrl+"/user/getUserObj?id={id}&name={name}&sex={sex}&age={age}",User.class,map);
        System.out.println("user4: "+user4.getBody());
        //get 接口是对象做参数 返回字符
        ResponseEntity<String> user5 = restTemplate.getForEntity(getUrl+"/user/getUserObj?id={id}&name={name}&sex={sex}&age={age}",String.class,map);
        System.out.println("user5: "+user5.getBody());

        //get 访问body作为参数 没有
//        User pUser=new User(6L,"小明6","男",24);
//        ResponseEntity<String> user6 = restTemplate.getForEntity(getUrl+"/user/getUserBody",String.class,pUser);
//        System.out.println("user6: "+user6.getBody());

//        restTemplate.postForEntity()

        return "ok";
    }


    @GetMapping("postRestUser")
    public String postRestUser(User u){

        //ForEntity：无参数访问 返回对象
        ResponseEntity<User> user0 = restTemplate.postForEntity(getUrl+"/user/postUser",null,User.class);
        System.out.println("user0: "+user0.getBody());
        //ForEntity：无参数访问 返回字符串
        ResponseEntity<String> user1 = restTemplate.postForEntity(getUrl+"/user/postUser",null,String.class);
        System.out.println("user1: "+user1.getBody());




        //参数
        MultiValueMap<String, Object> requestParam = new LinkedMultiValueMap<>();
        requestParam.set("id",5L);
        requestParam.set("name","小明5");
        requestParam.set("sex","男");
        requestParam.set("age",26);



        //ForEntity：基础传参方式 返回对象
        ResponseEntity<User> user0_1 = restTemplate.postForEntity(getUrl+"/user/postUserParam",requestParam,User.class);
        System.out.println("user0_1: "+user0_1.getBody());
        //ForEntity：基础传参方式 返回字符
        ResponseEntity<String> user0_2 = restTemplate.postForEntity(getUrl+"/user/postUserParam",requestParam,String.class);
        System.out.println("user0_2: "+user0_2.getBody());

        //ForEntity：自定义对象传参，返回对象
        ResponseEntity<User> user0_3 = restTemplate.postForEntity(getUrl+"/user/postUserObj",requestParam,User.class);
        System.out.println("user0_3: "+user0_3.getBody());
        //ForEntity：自定义对象传参，返回字符
        ResponseEntity<String> user0_4 = restTemplate.postForEntity(getUrl+"/user/postUserObj",requestParam,String.class);
        System.out.println("user0_4: "+user0_4.getBody());



        //ForObject: 可以直接返回相对的类型，
        //ForObject: 接口是基础类型参数，返回对象
        User user2 = restTemplate.postForObject(getUrl+"/user/postUserParam", requestParam, User.class);
        System.out.println("user2: "+user2);

        //ForObject: 接口是自定义对象参数，返回对象
        String user3 = restTemplate.postForObject(getUrl+"/user/postUserObj", requestParam, String.class);
        System.out.println("user3: "+user3);

        User user4 = restTemplate.postForObject(getUrl+"/user/postUserObj", requestParam, User.class);
        System.out.println("user4: "+user4);



        User user = new User(6L,"小明6",null,27);
        //ForObject: 接口参数是有 RequestBody 标记的，返回字符串
        String user5 = restTemplate.postForObject(getUrl+"/user/postUserBody", user, String.class);
        System.out.println("user5: "+user5);
        //ForObject: 接口参数是有 RequestBody 标记的，返回对象
        User user6 = restTemplate.postForObject(getUrl+"/user/postUserBody", user, User.class);
        System.out.println("user6: "+user6);



        return "OK";
    }
}
